(function() {
	window.login = {
		init: function() {
			this.telephone = /^\d{8,11}$/;
			this.pass = /^\S{6,12}$/
			login.skip()
			login.signin()
			if(localStorage.getItem("user") != null) {
				$("#account").val(localStorage.getItem("user"))
			}
			var wv = plus.webview.currentWebview('../loginregistration/login.html');
			// 关闭侧滑返回功能
			wv.setStyle({
				'popGesture': 'none'
			});
		},
		signin: function() {
			var first = null;
			mui.back = function() {
				//首次按键，提示‘再按一次退出应用’
				if(!first) {
					first = new Date().getTime();
					mui.toast('再按一次退出应用');
					setTimeout(function() {
						first = null;
					}, 1000);
				} else {
					if(new Date().getTime() - first < 1000) {
						plus.runtime.quit();
					}
				}
			};
		},
		skip: function() {

			//			
			//			var phonee = document.getElementById("account")
			//			var miaa= localStorage.getItem('mima')
			//			console.log(miaa)
			//			 phonee.value=miaa

			$("#forgetPassword").on("tap", function() {
				common.open("forgotpassword.html", "")
			})
			$("#registr").on("tap", function() {
				common.open("register.html", "")
			})
			$("#login").on('tap', function() {
				var phone = document.getElementById("account")
				var passwords = document.getElementById("password")
				if(!login.telephone.test(phone.value)) {
					mui.toast("8到11位账号!")
					return
				}
				if(!login.pass.test(passwords.value)) {
					mui.toast("请输入6到12位密码!")
					return
				}
				var clientId = '';
				switch(plus.os.name) {
					case "Android":
						clientId = plus.push.getClientInfo().clientid;
						break;
					case "iOS":
						clientId = plus.push.getClientInfo().token;
						break;
				}
				common.ajax("student/loginByUserAccount", {
					userAccount: phone.value,
					password: passwords.value,
					clientId: clientId,
					platform: plus.os.name
				}, function(data) {
					if(data.code == 200) {
						localStorage.setItem("uid", data.data.uid)
						localStorage.setItem("token", data.data.token)
						localStorage.setItem("data", JSON.stringify(data.data))
						localStorage.setItem("user", $("#account").val())
						localStorage.setItem("expireTime", data.data.expireTime)
						if(!data.data.hasBindPhone) {
							common.open("../home/authentication.html", "../home/authentication.html", "")
						} else if(!data.data.hasFillInfo) {
							common.open("message.html", "message.html")
						} else {
							common.open("../home/index.html", "index.html", "")
						}
					} else {
						mui.toast(data.message)
					}
				})
			})
		}
	}
	login.init()
})()